#include <QMenuBar>
#include <QStatusBar>
#include <QIcon>
#include <QMessageBox>
#include <QDockWidget>
#include <QListWidget>
#include <QPrinter>
#include <QDir>
#include <QInputDialog>
#include <QColorDialog>
#include <QCoreApplication>
#include <QSettings>

#include "stockanalyzer.h"


StockAnalyzer::StockAnalyzer(QWidget *parent) :
    QMainWindow(parent)
{
    setWindowState(Qt::WindowMaximized);
    setWindowIcon(QIcon(":/images/stockmarket.png"));
    setWindowTitle(tr("Stock Analyzer"));

    initMainWindow();



//    QString path = QCoreApplication::applicationDirPath() + "/data";
//    QFileInfo fileInfo(QDir(path), "StockAnalyzer.ini");
//    if(fileInfo.exists())
//    {
//        qDebug()<<"**********************";
//        QSettings settings(fileInfo.absoluteFilePath(), QSettings::IniFormat);
//        settings.beginGroup("stock code");
//        int count = settings.value("count").toInt();
//        for(int index = 0; index < count; index++)
//        {
//            QString stockCode = settings.value(QString::number(index)).toString();
//            m_stockCodeList.append(stockCode);
//        }
//    }
//    else
//    {
//        return false;
//    }





    m_pDataManagement = new DataManagement();

    m_pSetting = new StockSetting();
    connect(m_pSetting, SIGNAL(signalMicroDisplay(int, QFont, QColor, QPoint)),
            this, SLOT(slotMicroDisplay(int, QFont, QColor, QPoint)));
    connect(m_pSetting, SIGNAL(signalMicroDisplayClose()),
            this, SLOT(slotMicroDisplayClose()));
    m_pSetting->hide();


    m_microdisplayFlag = 201;

    if(m_microdisplayFlag == MIC_ALWAYS_DIS)
    {
        microdisplayActivated(true);
    }
    else
    {
        m_pMicroDisplay = 0;
    }


    connect(m_pDataManagement, SIGNAL(signalSendData(QHash<int, STOCKINFO>)), m_pCentralWidget,
            SLOT(slotUpdateStockInfo(QHash<int, STOCKINFO>)));
    connect(m_pDataManagement, SIGNAL(signalSendData(QHash<int, STOCKINFO>)), this,
            SLOT(slotProvideStockInfo(QHash<int, STOCKINFO>)));
    connect(this, SIGNAL(signalAddStock(int)), m_pDataManagement,
            SLOT(slotAddSelectedStock(int)));
    connect(this, SIGNAL(signalDelStock(int)), m_pDataManagement,
            SLOT(slotDeleteSelectedStock(int)));
}

StockAnalyzer::~StockAnalyzer()
{

}

void StockAnalyzer::initMainWindow()
{
    createActions();
    createMenus();
    createToolbars();
    createStatusBar();
    createCentralWindow();
    createDockWindows();
}

void StockAnalyzer::createActions()
{
    m_pNewAction = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
    m_pNewAction->setShortcuts(QKeySequence::New);
    m_pNewAction->setStatusTip(tr("Create a new..."));
    connect(m_pNewAction, SIGNAL(triggered()), this, SLOT(slotNew()));

    m_pSaveAction = new QAction(QIcon(":/images/save.png"), tr("&Save..."), this);
    m_pSaveAction->setShortcuts(QKeySequence::Save);
    m_pSaveAction->setStatusTip(tr("Save..."));
    connect(m_pSaveAction, SIGNAL(triggered()), this, SLOT(slotSave()));

    m_pAddAction = new QAction(QIcon(":/images/add.png"), tr("&Add..."), this);
    m_pAddAction->setShortcuts(QKeySequence::AddTab);
    m_pAddAction->setStatusTip(tr("Add..."));
    connect(m_pAddAction, SIGNAL(triggered()), this, SLOT(slotAddStock()));

    m_pDelAction = new QAction(QIcon(":/images/del.png"), tr("&Delete..."), this);
    m_pDelAction->setShortcuts(QKeySequence::Delete);
    m_pDelAction->setStatusTip(tr("Del..."));
    connect(m_pDelAction, SIGNAL(triggered()), this, SLOT(slotDelStock()));

    m_pQuitAction = new QAction(tr("&Quit"), this);
    m_pQuitAction->setShortcuts(QKeySequence::Quit);
    m_pQuitAction->setStatusTip(tr("Quit the application"));
    connect(m_pQuitAction, SIGNAL(triggered()), this, SLOT(close()));

    m_pUndoAction = new QAction(QIcon(":/images/undo.png"), tr("&Undo"), this);
    m_pUndoAction->setShortcuts(QKeySequence::Undo);
    m_pUndoAction->setStatusTip(tr("Undo the last editing action"));
    connect(m_pUndoAction, SIGNAL(triggered()), this, SLOT(slotUndo()));

    m_pSettingAction = new QAction(QIcon(":/images/setting.png"), tr("&Setting"), this);
//    m_pSettingAction->setShortcuts(QKeySequence::Undo);
    m_pSettingAction->setStatusTip(tr("setting..."));
    connect(m_pSettingAction, SIGNAL(triggered()), this, SLOT(slotSetting()));

    m_pAboutAction = new QAction(tr("&About"), this);
    m_pAboutAction->setStatusTip(tr("Show the application's About box"));
    connect(m_pAboutAction, SIGNAL(triggered()), this, SLOT(slotAbout()));
}

void StockAnalyzer::createMenus()
{

    m_pFileMenu = menuBar()->addMenu(tr("&File"));
    m_pFileMenu->addAction(m_pNewAction);
    m_pFileMenu->addAction(m_pSaveAction);
    m_pFileMenu->addAction(m_pAddAction);
    m_pFileMenu->addAction(m_pDelAction);
    m_pFileMenu->addSeparator();
    m_pFileMenu->addAction(m_pQuitAction);

    m_pEditMenu = menuBar()->addMenu(tr("&Edit"));
    m_pEditMenu->addAction(m_pUndoAction);

    m_pToolMenu = menuBar()->addMenu(tr("&Tool"));
    m_pToolMenu->addAction(m_pSettingAction);

    m_pHelpMenu = menuBar()->addMenu(tr("&Help"));
    m_pHelpMenu->addAction(m_pAboutAction);
}

void StockAnalyzer::createToolbars()
{
    m_pFileToolbar = addToolBar(tr("&File"));
    m_pFileToolbar->addAction(m_pNewAction);
    m_pFileToolbar->addAction(m_pSaveAction);
    m_pFileToolbar->addAction(m_pAddAction);
    m_pFileToolbar->addAction(m_pDelAction);

    m_pEditToolbar = addToolBar(tr("&Edit"));
    m_pEditToolbar->addAction(m_pUndoAction);

    m_pToolToolbar = addToolBar(tr("&Tool"));
    m_pToolToolbar->addAction(m_pSettingAction);
}

void StockAnalyzer::createStatusBar()
{
    statusBar()->showMessage(tr("Ready"), 2000);
}

void StockAnalyzer::createCentralWindow()
{
    m_pCentralWidget = new CentralWidget;
    m_pCentralWidget->setMinimumSize(300,200);
    setCentralWidget(m_pCentralWidget);
}

void StockAnalyzer::createDockWindows()
{
    QDockWidget *dockwidget0 = new QDockWidget(tr("title0"), this);
    QListWidget *listwidget0 = new QListWidget(dockwidget0);
    dockwidget0->setWidget(listwidget0);
    addDockWidget(Qt::LeftDockWidgetArea, dockwidget0,Qt::Horizontal);


    QDockWidget *dockwidget1 = new QDockWidget(tr("title1"), this);
    QListWidget *listwidget1 = new QListWidget(dockwidget1);
    dockwidget1->setWidget(listwidget1);
    addDockWidget(Qt::RightDockWidgetArea, dockwidget1,Qt::Vertical);

    QDockWidget *dockwidget2 = new QDockWidget(tr("title2"), this);
    QListWidget *listwidget2 = new QListWidget(dockwidget2);
    dockwidget2->setWidget(listwidget2);
    addDockWidget(Qt::BottomDockWidgetArea, dockwidget2,Qt::Horizontal);
}

void StockAnalyzer::slotNew()
{
    statusBar()->showMessage(tr("new..."));
}

void StockAnalyzer::slotSave()
{
    statusBar()->showMessage(tr("save..."));
}

void StockAnalyzer::slotAddStock()
{
    statusBar()->showMessage(tr("Add selected stock..."));

    bool ok;
    int code = QInputDialog::getInt(this, tr("加入自选股"),
                                         tr("请输入股票代码:"),
                                         0000001, 000001, 699999, 1,
                                         &ok, Qt::Dialog);
    qDebug()<<"***********slotAddStock->code:"<<code;
    if (ok)
        emit signalAddStock(code);
}

void StockAnalyzer::slotDelStock()
{
    int index = m_pCentralWidget->getCurrentRow();
    emit signalDelStock(index);
}

void StockAnalyzer::slotUndo()
{
    statusBar()->showMessage(tr("undo..."));
}

void StockAnalyzer::microdisplayActivated(bool b)
{
    if(b)
    {
        if(m_pMicroDisplay == 0)
        {
            m_pMicroDisplay = new MicroDisplay;

            m_pMicroDisplay->setDisplayParameter(m_MDFont, m_MDColor, m_MDPoint);

            connect(m_pSetting, SIGNAL(signalChangedMDColor(QColor)), m_pMicroDisplay,
                    SLOT(slotSetDisplayColor(QColor)));
            connect(m_pSetting, SIGNAL(signalChangedMDFont(QFont)), m_pMicroDisplay,
                    SLOT(slotSetDisplayFont(QFont)));
            connect(m_pSetting, SIGNAL(signalChangedMDPosition(QPoint)),
                    m_pMicroDisplay, SLOT(slotSetDisplayPosition(QPoint)));
        }
    }
    else
    {

        if(m_pMicroDisplay != 0)
        {
            disconnect(m_pSetting, SIGNAL(signalChangedColor(QColor)), m_pMicroDisplay,
                    SLOT(slotSetDisplayColor(QColor)));
            disconnect(m_pSetting, SIGNAL(signalChangedFont(QFont)), m_pMicroDisplay,
                    SLOT(slotSetDisplayFont(QFont)));
            disconnect(m_pSetting, SIGNAL(signalChangedPosition(QPoint)),
                    m_pMicroDisplay, SLOT(slotSetDisplayPosition(QPoint)));

            delete m_pMicroDisplay;
            m_pMicroDisplay = 0;
        }
    }

}

void StockAnalyzer::slotSetting()
{
    if(m_pMicroDisplay != 0)
    {
        QFont font = m_pMicroDisplay->getDisplayFont();
        QColor color = m_pMicroDisplay->getDisplayColor();
        QPoint point = m_pMicroDisplay->getDisplayPosition();

        m_pSetting->initMicroDisplaySetting(m_microdisplayFlag, font, color, point);
    }

    m_pSetting->show();
    m_pSetting->raise();
}

void StockAnalyzer::slotAbout()
{
    QMessageBox::about(this, tr("About Stock Analyzer"),
             tr("The version number is 1.0"));
}

void StockAnalyzer::slotPressedHotkey(int value)
{
    qDebug()<<"============================================================:"<<value;

    if(isHidden())
    {
        this->showMaximized();
        this->raise();

        if(m_microdisplayFlag == MIC_HIDE_DIS)
        {
            microdisplayActivated(false);
        }
    }
    else
    {
        this->hide();
    }

}

void StockAnalyzer::slotProvideStockInfo(QHash<int, STOCKINFO> stockInfoHash)
{

}

void StockAnalyzer::slotMicroDisplay(int flag, QFont font, QColor color, QPoint point)
{
    qDebug()<<"++++++++++++++++++++++++++++++++++++++++++++++++value:"<<flag;

    m_MDFont = font;
    m_MDColor = color;
    m_MDPoint = point;

    if(flag == MIC_HIDE_DIS)
    {
        m_microdisplayFlag = MIC_HIDE_DIS;
        microdisplayActivated(false);
    }
    else
    {
        m_microdisplayFlag = MIC_ALWAYS_DIS;
        microdisplayActivated(true);
    }
}

void StockAnalyzer::slotMicroDisplayClose()
{
    m_microdisplayFlag = MIC_NERVER_DIS;
    microdisplayActivated(false);
}

void StockAnalyzer::closeEvent(QCloseEvent *event)
{

    if(m_pDataManagement != NULL)
    {
        m_pDataManagement->saveStockData();
    }

    emit signalCloseWindow();
}

void StockAnalyzer::changeEvent(QEvent * event)
{

    if(isActiveWindow())
    {
        if(m_microdisplayFlag == MIC_HIDE_DIS)
        {
            microdisplayActivated(false);
        }
    }
    else
    {
        if(isMinimized() || isHidden())
        {
            qDebug()<<"+++++++++++++++++++++++isHidden++++++++++++++++++++";
            if(m_microdisplayFlag == MIC_HIDE_DIS)
            {
                qDebug()<<"+++++++++++++++++++++++MIC_HIDE_DIS++++++++++++++++++++";
                microdisplayActivated(true);
            }
        }
    }

}
